import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { OperationModel } from '#/api/process/models';
import type { DictModel } from '#/api/system/models';

import { isUndefined } from '@vben/utils';

import { getWorkstationListApi } from '#/api/master/workstation';
import { useOperationCategoryOptions } from '#/api/process/models';
import { getDictByCodeApi } from '#/api/system/dict';

const _mapping: Record<string, string> = {};

/**
 * 获取编辑表单的字段配置。
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      dependencies: {
        triggerFields: ['opCode'],
        disabled(values) {
          return !isUndefined(values.id);
        },
      },
      fieldName: 'opCode',
      label: '工序代码',
      rules: 'required',
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'Input',
      fieldName: 'opName',
      label: '工序名称',
      rules: 'required',
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        mode: 'multiple',
        showSearch: true,
        optionFilterProp: 'label',
        labelField: 'wsName',
        valueField: 'id',
        api: getWorkstationListApi,
        immediate: false,
      },
      fieldName: 'wsIds',
      label: '工位',
      rules: 'required',
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        afterFetch: (data: DictModel) => {
          data.items.forEach((item) => {
            _mapping[item.code] = item.label;
          });
          return data.items.map((item) => ({
            label: item.label,
            value: item.code,
          }));
        },
        api: getDictByCodeApi,
        params: { code: 'mes_process_type' },
      },
      dependencies: {
        trigger(values, form) {
          form.setFieldValue(
            'processTypeName',
            _mapping[values.processTypeCode],
          );
        },
        triggerFields: ['processTypeCode'],
      },
      fieldName: 'processTypeCode',
      label: '工艺类型',
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        options: useOperationCategoryOptions(),
      },
      fieldName: 'processCategory',
      label: '工艺分类',
      rules: 'required',
      defaultValue: 1,
      help: '上、下料工序若无统计要求，一般归属于运输工艺',
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'Input',
      fieldName: 'workCenter',
      label: '工作中心',
      help: '如区域、设备等',
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'Input',
      fieldName: 'tag',
      label: '其他信息',
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'InputNumber',
      componentProps: {
        min: 1,
        precision: 0,
        step: 10,
      },
      fieldName: 'order',
      label: '排序',
      defaultValue: 100,
      rules: 'required',
    },
    {
      component: 'Switch',
      componentProps: {
        checkedValue: 1,
        unCheckedValue: 0,
        checkedChildren: '启用',
        unCheckedChildren: '禁用',
        class: 'w-auto',
      },
      fieldName: 'status',
      label: '状态',
      defaultValue: 1,
      rules: 'required',
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<OperationModel>,
): VxeTableGridOptions<OperationModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'opCode', title: '工序代码', width: 120 },
    { field: 'opName', title: '工序名称', width: 140 },
    {
      field: 'processCategory',
      title: '工艺分类',
      width: 120,
      cellRender: {
        name: 'CellTag',
        options: useOperationCategoryOptions(),
      },
    },
    { field: 'processTypeName', title: '工艺类型', width: 120 },
    {
      field: 'workstations',
      title: '工位',
      width: 180,
      slots: { default: 'workstations' },
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: { name: 'CellTag', attrs: { useStatus: true } },
    },
    { field: 'workCenter', title: '工作中心', width: 120 },
    { field: 'tag', title: '其他信息', width: 120 },
    { field: 'order', title: '排序', width: 80 },
    { field: 'createdTime', title: '创建时间', minWidth: 160 },
    {
      cellRender: {
        attrs: {
          nameField: 'opName',
          nameTitle: '工序',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: ['edit', 'delete'],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 120,
    },
  ];
}
